<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_menu_type.class.php');


class TCMS_Menu_Type_DBC extends TCMS_Menu_Type implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select name, ".
						"class_path, ".
						"class_name, ".
						"page_manage_path ".
					"from tcms_menu_types where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_name(stripslashes($rez_obj->NAME));
			$this->set_class_path(stripslashes($rez_obj->CLASS_PATH));
			$this->set_class_name(stripslashes($rez_obj->CLASS_NAME));
			$this->set_page_manage_path(stripslashes($rez_obj->PAGE_MANAGE_PATH));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_menu_types_seq');
		$name = addslashes($this->get_name());
		$class_path = addslashes($this->get_class_path());
		$class_name = addslashes($this->get_class_name());
		$page_manage_path = addslashes($this->get_page_manage_path());

		if ($id) {
			$sql = "insert into tcms_menu_types (id, ".
									      "name, ".
									      "class_path, ".
									      "class_name, ".
									      "page_manage_path ".
									      ") ".
								"values ".
									      "($id,".
									      "'$name', ".
									      "'$class_path', ".
									      "'$class_name', ".
									      "'$page_manage_path' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_menu_types_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$name = addslashes($this->get_name());
		$class_path = addslashes($this->get_class_path());
		$class_name = addslashes($this->get_class_name());
		$page_manage_path = addslashes($this->get_page_manage_path());

		$sql = "update tcms_menu_types set ".
						"name = '$name', ".
						"class_path = '$class_path', ".
						"class_name = '$class_name', ".
						"page_manage_path = '$page_manage_path' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_menu_types where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid() {
		$sql = "select id, ".
						"name ".
					"from tcms_menu_types order by name asc";

		return $sql;
	}


	public static function	get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from tcms_menu_types";

		return $sql;
	}


	public static function load_types_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from tcms_menu_types order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}

		return $ret;
	}


	public function load_by_hid(DB_Connection $dbc, $hid) {
		$hid = addslashes($hid);
		$ret = false;

		$sql = "select id from tcms_menu_types where hid = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}
}